package com.gksc.video.schedule;

import cn.hutool.core.date.DateUtil;
import com.gksc.video.config.GkzfWebProperties;
import com.gksc.video.constant.CacheConstant;
import com.gksc.video.data.DeviceInfo;
import com.gksc.video.manager.BasePushThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @className: SystemCheckSelfJob
 * @description: 系统自检任务实现类
 * @author: Liao.yx
 * @email: 1229053515@qq.com
 * @create: 2021年05月14日 16时46分
 * @copyRight: 2020 liaoyongxiang All rights reserved.
 **/
@Slf4j
@Component
public class SystemCheckSelfJob implements CommandLineRunner {
    public static Timer systemCheckSelfJob;
    @Autowired
    private GkzfWebProperties properties;

    @Override
    public void run(String... args) throws Exception {
        // 超过5分钟，结束推流
        systemCheckSelfJob = new Timer("SystemCheckSelfJob");
        systemCheckSelfJob.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("定时任务-当前有" + CacheConstant.JOBMAP.size() + "个推流任务正在进行推流");
                // 管理缓存
                if (null != CacheConstant.STREATMAP && 0 != CacheConstant.STREATMAP.size()) {
                    for (String key : CacheConstant.STREATMAP.keySet()) {
                        DeviceInfo device = CacheConstant.STREATMAP.get(key);
                        BasePushThread task = CacheConstant.JOBMAP.get(key);
                        try {
                            if (!ObjectUtils.isEmpty(device) && !ObjectUtils.isEmpty(task)) {
                                if (device.getCount() == 0) {
                                    task.cancel();
                                    CacheConstant.STREATMAP.remove(key);
                                    CacheConstant.JOBMAP.remove(key);
                                    log.info("定时任务{}当前设备使用人数为0结束推流 设备信息：{}", key, device.toString());
                                } else if (!ObjectUtils.isEmpty(device.getOpenTime())) {
                                    if (DateUtil.offsetMinute(device.getOpenTime(), properties.getPushCfg().getKeepAliveMinute()).before(new Date())) {
                                        task.cancel();
                                        CacheConstant.STREATMAP.remove(key);
                                        CacheConstant.JOBMAP.remove(key);
                                        log.info("定时任务{}当前设备使用时间超时结束推流 设备信息：{}", key, device.toString());
                                    }
                                }
                            } else {
                                log.info("定时任务{}当前设备推流失败，原因是数据有误 设备信息：{}", key, device.toString());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error("执行系统自检推流任务异常，异常原因是", e);
                        }
                    }
                }
            }
        }, 1, 1000 * 60);
    }
}
